//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：UIFrameAutomation
// *描述：框架自动运行脚本
// *?2021 MYJL . All rights reserved.
//* ======================================================================
using System;
using UnityEngine;
using Object = UnityEngine.Object;

namespace HDMCode.UIFrame
{
    public class HDMUI
    {
        /// <summary>
        /// 当前运行的UI框架实时节点管理
        /// </summary>
        public static UIFrameRuntime Runtime {get; private set; }

        /// <summary>
        /// UI框架内置的工具方法
        /// </summary>
        public static UIFrameHelper Helper {get; private set; }
        
        /// <summary>
        /// UI框架行为逻辑脚本
        /// </summary>
        public static UIFrameViewBehavior Behavior {get; private set; }
        
        /// <summary>
        /// UI框架入口脚本
        /// </summary>
        public static UIEntityEntry UI {get; private set; }
        
        /// <summary>
        /// 初始化UI框架
        /// </summary>
        public static void InitUIFrame()
        {
            if (UI == null)
            {
                try
                {
                    GameObject ui = new GameObject("UIFrameWork");
                    Runtime = ui.AddComponent<UIFrameRuntime>();
                    Helper = ui.AddComponent<UIFrameHelper>();
                    Behavior = ui.AddComponent<UIFrameViewBehavior>();
                    UI = new UIEntityEntry();
                    Runtime.hideFlags = HideFlags.HideInInspector;
                    Object.DontDestroyOnLoad(ui);
                    UIFrame.Log("UI框架初始化完成！", "手动初始化", LogSystem.UIModle);
#if (UNITY_EDITOR)
                    Application.quitting += QuitCallBack;
#endif
                }
                catch (Exception error)
                {
                    UIFrame.LogError("UI框架初始化失败！", error, LogSystem.UIModle);
                }
            }
        }
        
#if (UNITY_EDITOR)
        public static void QuitCallBack() => UI = null;
#endif
    }
}


